package io.yunho.yunho.interview.two.juc;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * Created by Administrator on 2020/4/13.
 *  ABA问题的解决 AtomicStampedReference
 */
public class ABASolutionDemo {
    static AtomicReference<Integer> atomicReference= new AtomicReference<>(100);
    static AtomicStampedReference<Integer> atomicStampedReference = new AtomicStampedReference<>(100,1);
    public static void main(String[] args) {
        System.out.println("============一以下是ABA问题的产生==============");
        new Thread(()->{
            System.out.println(atomicReference.compareAndSet(100, 101)+"\t "+Thread.currentThread().getName()+" 当前值："+atomicReference.get());
            System.out.println(atomicReference.compareAndSet(101, 100)+"\t "+Thread.currentThread().getName()+" 当前值："+atomicReference.get());
        },"t1").start();

        new Thread(()->{
            //暂停一秒 保证t1线程完成ABA
            try {
                TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}
            System.out.println(atomicReference.compareAndSet(100, 2019)+"\t "+Thread.currentThread().getName()+" 当前值："+atomicReference.get());
        },"t2").start();
        //暂停2秒钟
        try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}
        System.out.println("============一以下是ABA问题的解决==============");

        new Thread(()->{
            int stamp = atomicStampedReference.getStamp();
            System.out.println(Thread.currentThread().getName()+"\t第1次版本："+stamp);
            //暂停1秒 t3
            try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}
            System.out.println(atomicStampedReference.compareAndSet(100, 101,atomicStampedReference.getStamp(),atomicStampedReference.getStamp()+1)+"\t "+Thread.currentThread().getName()+" 当前值："+atomicStampedReference.getReference());
            System.out.println(Thread.currentThread().getName()+"\t第2次版本："+atomicStampedReference.getStamp());
            System.out.println(atomicStampedReference.compareAndSet(101, 100,atomicStampedReference.getStamp(),atomicStampedReference.getStamp()+1)+"\t "+Thread.currentThread().getName()+" 当前值："+atomicStampedReference.getReference());
            System.out.println(Thread.currentThread().getName()+"\t第3次版本："+atomicStampedReference.getStamp());
            },"t3").start();

        new Thread(()->{
            int stamp = atomicStampedReference.getStamp();
            System.out.println(Thread.currentThread().getName()+"\t第一次版本："+stamp);
            //暂停3秒 保证t4线程完成ABA
            try {
                TimeUnit.SECONDS.sleep(3);} catch (InterruptedException e) {e.printStackTrace();}
            System.out.println(atomicStampedReference.compareAndSet(100, 2019, stamp, stamp + 1)
                    +"\t"+Thread.currentThread().getName()+"\t 当前值:"+atomicStampedReference.getReference()
                    +"\t当前版本号："+atomicStampedReference.getStamp());
        },"t4").start();
        //暂停2秒钟
        try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}
    }
}
